package de.edb.ctrl;

import java.io.IOException;
import java.util.Date;

import org.apache.catalina.util.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import de.edb.dao.ArtistDao;
import de.edb.dao.DaoException;
import de.edb.model.Artist;

@Controller
public class ArtistController {

	private ArtistDao artistDao = null;

    public void setArtistDao(ArtistDao artistDao) {
		this.artistDao = artistDao;
	}

    @RequestMapping(value="/admin/editartist.html", method=RequestMethod.GET)
    public ModelAndView edit(@RequestParam(required=false) Integer id) {
    	ModelAndView mv = new ModelAndView();
    	if (id == null) {
    		mv.addObject(new Artist());
    	} else { 
    		mv.addObject(artistDao.findById(id));
    	}
    	mv.addObject("tab", "artists");
    	mv.setViewName("edit-artist");
    	return mv;
    }
    
        
    /**
     * <p>Saves an Artist.</p>
     * 
     * <p>Expected HTTP POST and request '/saveartist.html'.</p>
     * @return 
     * @throws DaoException 
     */
    @RequestMapping(value="/admin/saveartist.html", method=RequestMethod.POST)
    public ModelAndView save(Artist artist, Model model, @RequestParam(value="pic", required=true) MultipartFile pic) {
    	try{
    		String newPicture = Base64.encode(pic.getBytes());
    		if(newPicture.length()>10) {
    			artist.setPicture(newPicture);
    		}
    	} catch(IOException ex) {
    		System.out.print(ex.getStackTrace());
    	}
    	
    	String yt = artist.getYoutube();
    	if(yt.length() != 11) artist.setYoutube("");
    	
    	
    	String hp = artist.getHomepage();
    	if(hp.length() > 8 && !(hp.toLowerCase().startsWith("http://")) && !(hp.toLowerCase().startsWith("https://"))) artist.setHomepage("http://"+hp);
    	
    	try {
    	
	        if (artist.getCreated() == null) {
	            artist.setCreated(new Date());
	        }
	        
	        Artist result = artistDao.save(artist);
	        if (artist.getId() == null) {
	            artist.setId(result.getId());
	        }
	
	        return new ModelAndView("redirect:/artists.html");
    	} catch (DaoException e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			mv.addObject("tab", "artists");
			return mv;
    	}
    }

    /**
     * <p>Deletes an artist.</p>
     * 
     * <p>Expected HTTP POST and request '/deleteartist.html'.</p>
     * @throws DaoException 
     */
    @RequestMapping(value="/admin/deleteartist.html", method=RequestMethod.GET)
    public ModelAndView delete(Artist artist) {
        try {
			artistDao.delete(artist);
			return new ModelAndView("redirect:/artists.html");
		} catch (DaoException e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			mv.addObject("tab", "artists");
			return mv;
		}
    }

    /**
     * <p>Searches for all persons and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/artists.html'.</p>
     */
    @RequestMapping(value="/artists.html", method=RequestMethod.GET)
    public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Künstler");
		mv.addObject("message", "Alle Künstler");
		mv.addObject("artists", artistDao.findAll());
		mv.addObject("tab", "artists");
		mv.setViewName("list-artists");
		return mv;
    }
    
    
    /**
     * <p>Searches for all artists and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/AllePersonen.html'.</p>
     */
    @RequestMapping(value="/artist.html", method=RequestMethod.GET)
    public ModelAndView show(@RequestParam(required=true) Integer id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("artist", artistDao.findById(id));
		mv.addObject("tab", "artists");
		mv.setViewName("show-artist");
		return mv;
    }

}
